/**********************************************************************************************/
/* FBL_StubRegistry.h	                                                    				  */
/*                                                                       					  */
/* Copyright Paradigma, 1993-2004															  */
/* All Rights Reserved                                                   					  */
/**********************************************************************************************/

#ifndef _FBL_StubRegistry_h 
	#define _FBL_StubRegistry_h
#pragma once

#include <VShared/FBL/publ/Headers/FBL.h>

#include <VShared/FBL/publ/Interfaces/FBL_I_Unknown.h>
//#include <VShared/FBL/publ/Interfaces/FBL_I_Connectable.h>

// FINAL:
#include <VShared/FBL/publ/Headers/FBL_pre_header.h>

#undef new

// STD:
#include <map>

//
#if DEBUG_NEW > 0
#define new NEW
#endif //DEBUG_NEW > 0


#ifdef __MWERKS__
#include <stdlib.h>
#endif //__MWERKS__



/**********************************************************************************************/
FBL_Begin_Namespace
interface I_Connectable;
interface I_Connection;
SMART_INTERFACE(I_Connection);

/**********************************************************************************************/
// clients:
// Key		- ulong (proxy-object address)
// Value	- I_Connectable_Ptr (stub-object address)
//
// Server:
// Key		- I_Unknown_Ptr (kernel-object address)
// Value	- ulong (proxy-object address)
//
template <class TKey, class TValue> class StubRegistry : public I_Unknown
{
		IMPLEMENT_UNKNOWN(StubRegistry)											
		BEGIN_INTERFACE_TABLE(StubRegistry)
		END_INTERFACE_TABLE()
		
	public://///////////////////////////////////////////////////////////////////////////////////

//	typedef std::map<TKey, TValue>			 TStubMap;
#ifdef V_SERVER
	typedef std::map<ulong, ulong>			TStubMap;
#else	
	typedef std::map<ulong, I_Connectable*>			TStubMap;
#endif // V_SERVER

		

	public://///////////////////////////////////////////////////////////////////////////////////

							StubRegistry( void )
							{
							}

virtual		 				~StubRegistry( void )
							{
							}
				
					
	public://///////////////////////////////////////////////////////////////////////////////////

#ifdef V_SERVER
		void				Register( TKey inKey, TValue inStubPtr ) throw()
							{
								if( inKey )
									mStubMap.insert( 
										std::make_pair( 
											inKey, 
											inStubPtr 
											) );
							}
#else
		void				Register( TValue inStubPtr ) throw()
							{
								if( inStubPtr )
									mStubMap.insert( 
										std::make_pair( 
											inStubPtr->get_RemoteID(), 
											inStubPtr ) );
							}
#endif // V_SERVER

		void				Unregister( TKey inKey ) throw()
							{
								if( inKey )
								{
#ifdef V_SERVER
									mStubMap.erase( inKey );
#else
									mStubMap.erase( inKey );
#endif // V_SERVER
								}
							}


		TValue				Find( TKey inStubName ) throw()
							{
								TStubMap::iterator i = mStubMap.find(inStubName);
								if( i == mStubMap.end() )
									return NULL;

								return i->second;
							}

//#if DEBUG_NEW
		int					GetCount( void ) { return mStubMap.size(); }
//#endif

		void				Erase( void ) throw()
							{
								mStubMap.clear();
							}


	private:////////////////////////////////////////////////////////////////////////////////////

		TStubMap			mStubMap;
};


typedef StubRegistry<ulong, I_Connectable*> ClientStubRegistry;
typedef StubRegistry<ulong, ulong> ServerStubRegistry;

FBL_SMART_PTR	( ClientStubRegistry );
FBL_SMART_PTR	( ServerStubRegistry );


/**********************************************************************************************/
ClientStubRegistry* GetClientStubRegistry( I_Connection_Ptr		inConnection );


/**********************************************************************************************/
FBL_End_Namespace

#include <VShared/FBL/publ/Headers/FBL_post_header.h>

#endif // _FBL_StubRegistry_h
